package com.liaowei.post.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.Post;
import com.liaowei.model.vo.PostVO;
import com.liaowei.post.model.param.PostPageParam;
import com.liaowei.post.model.param.SavePostParam;
import com.liaowei.post.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Validated
public class PostController {

    private final PostService postService;


    // 查询帖子相关推荐列表 -- 取前5条
    @GetMapping("recommend/{userId}")
    public BaseResponse<List<PostVO>> recommendPostList(@PathVariable Long userId) {
        return ResultUtils.success(postService.searchRecommendPostList(userId));
    }

    // 查询自己发布的帖子
    @GetMapping("ownPost")
    public BaseResponse<List<Post>> ownPostList(@RequestParam @NotNull(message = "用户名不能为空") Long userId,
                                                  String title) {
        return ResultUtils.success(postService.searchOwnPostList(userId,title));
    }

    // 查询贴吧热议榜
    @GetMapping("hotPost")
    public BaseResponse<List<PostVO>> hotPostList() {
        return ResultUtils.success(postService.searchHotPostList());
    }

    // 根据userId查询帖子数量
    @GetMapping("count/{userId}")
    public BaseResponse<Long> countOfPostByUserId(@PathVariable
                                                  @Min(value = 1, message = "请输入合法的id")
                                                  Long userId) {
        return ResultUtils.success(postService.searchCountOfPostByUserId(userId));
    }

    // 删除帖子
    @DeleteMapping("{postId}/{userId}")
    public BaseResponse<String> removePost(@PathVariable
                                           @Min(value = 1, message = "请输入合法id")
                                           Long postId,
                                           @PathVariable
                                           @Min(value = 1, message = "请输入合法id")
                                           Long userId) {
        return ResultUtils.success(postService.removePostByIdAndUserId(postId, userId));
    }

    // 根据id查询帖子
    @GetMapping("{postId}")
    public BaseResponse<Post> postVoById(@PathVariable
                                         @Min(value = 1, message = "请输入合法的id")
                                         Long postId) {
        return ResultUtils.success(postService.searchPostVoById(postId));
    }

    // 查询帖子详细信息以及和用户的状态
    @GetMapping("{postId}/{userId}")
    public BaseResponse<PostVO> postVo(@PathVariable
                                       @Min(value = 1, message = "请输入合法的id")
                                       Long postId,
                                       @PathVariable
                                       @Min(value = 1, message = "请输入合法的id")
                                       Long userId) {
        return ResultUtils.success(postService.searchPostVo(postId, userId));
    }

    // 通过id列表查询帖子列表
    @PostMapping("pageByIds")
    BaseResponse<List<Post>> pageByIds(@RequestBody List<Long> postIds){
        return ResultUtils.success(postService.searchPostListByIds(postIds));
    }

    // 通过标题分页查询帖子列表
    @GetMapping("pageByTitle")
    public BaseResponse<IPage<Post>> pageByTitle(@RequestParam String title,
                                                 @RequestParam int pageNum,
                                                 @RequestParam int pageSize) {
        return ResultUtils.success(postService.searchPostPageByTitle(title,pageNum,pageSize));
    }

    // 条件分页查询帖子列表
    @PostMapping("page")
    public BaseResponse<IPage<Post>> page(@RequestBody @Validated PostPageParam pageParam) {
        return ResultUtils.success(postService.searchPostPage(pageParam));
    }

    // 保存发帖信息
    @PostMapping
    public BaseResponse<String> savePost(@RequestBody @Validated SavePostParam postParam) {
        return ResultUtils.success(postService.savePost(postParam));
    }

}
